{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                 date  Year  Month  Day  Hour  Week   WeekDay\n",
      "0 2022-10-24 13:21:00  2022     10   24    13    43    Monday\n",
      "1 2021-12-31 07:32:11  2021     12   31     7    52    Friday\n",
      "2 2019-07-25 00:00:00  2019      7   25     0    30  Thursday\n",
      "3 2022-01-24 00:00:00  2022      1   24     0     4    Monday\n",
      "4 2023-12-07 16:02:51  2023     12    7    16    49  Thursday\n",
      "5 2022-10-01 00:00:00  2022     10    1     0    39  Saturday\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 这里支持各种合法形式的时间输入：\n",
    "# YYYY-MM-DD | MM/DD/YYYY HH:MM:SS 等\n",
    "# 没有明确小时/分钟的，都按照0时0分\n",
    "# 没有明确日期的，都默认1号\n",
    "\n",
    "df = pd.DataFrame(['2022-10-24 13:21:00',\"2021-12-31 07:32:11\",\\\n",
    "    \"2019-07-25\", '2022-1-24', \"12/7/2023 16:2:51\", \\\n",
    "        \"2022-10\"], columns=[\"date\"])\n",
    "# 转换为时间\n",
    "df[\"date\"] = pd.to_datetime(df[\"date\"])\n",
    "# 获取年\n",
    "df[\"Year\"] = df[\"date\"].dt.year\n",
    "# 获取月\n",
    "df[\"Month\"] = df[\"date\"].dt.month\n",
    "# 获取日\n",
    "df[\"Day\"] = df[\"date\"].dt.day\n",
    "# 获取当前小时 \n",
    "df[\"Hour\"] = df[\"date\"].dt.hour\n",
    "# 获取周(一年中的第几周)\n",
    "df['Week'] = df['date'].dt.isocalendar().week\n",
    "# 获取这一天是星期几\n",
    "df['WeekDay'] = df['date'].dt.day_name()\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>WeekDay</th>\n",
       "      <th>Quarter</th>\n",
       "      <th>Interval</th>\n",
       "      <th>DayOfYear</th>\n",
       "      <th>IsWeekEnd</th>\n",
       "      <th>isWorking</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2022-10-24 13:21:00</td>\n",
       "      <td>Monday</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>297</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2021-12-31 07:32:11</td>\n",
       "      <td>Friday</td>\n",
       "      <td>4</td>\n",
       "      <td>297</td>\n",
       "      <td>365</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2019-07-25 00:00:00</td>\n",
       "      <td>Thursday</td>\n",
       "      <td>3</td>\n",
       "      <td>1187</td>\n",
       "      <td>206</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2022-01-24 00:00:00</td>\n",
       "      <td>Monday</td>\n",
       "      <td>1</td>\n",
       "      <td>273</td>\n",
       "      <td>24</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2023-12-07 16:02:51</td>\n",
       "      <td>Thursday</td>\n",
       "      <td>4</td>\n",
       "      <td>-409</td>\n",
       "      <td>341</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2022-10-01 00:00:00</td>\n",
       "      <td>Saturday</td>\n",
       "      <td>4</td>\n",
       "      <td>23</td>\n",
       "      <td>274</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 date   WeekDay  Quarter  Interval  DayOfYear  IsWeekEnd  \\\n",
       "0 2022-10-24 13:21:00    Monday        4         0        297      False   \n",
       "1 2021-12-31 07:32:11    Friday        4       297        365      False   \n",
       "2 2019-07-25 00:00:00  Thursday        3      1187        206      False   \n",
       "3 2022-01-24 00:00:00    Monday        1       273         24      False   \n",
       "4 2023-12-07 16:02:51  Thursday        4      -409        341      False   \n",
       "5 2022-10-01 00:00:00  Saturday        4        23        274       True   \n",
       "\n",
       "  isWorking  \n",
       "0      True  \n",
       "1       NaN  \n",
       "2       NaN  \n",
       "3       NaN  \n",
       "4      True  \n",
       "5       NaN  "
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取季度\n",
    "df['Quarter']=df['date'].dt.quarter\n",
    "# 获取距离今天的时间差\n",
    "df['Interval'] = list(map(lambda x: \\\n",
    "    x.days, pd.to_datetime('today') - df['date']))\n",
    "# 这一天是一年中的第几天\n",
    "df['DayOfYear'] = df['date'].dt.dayofyear\n",
    "# 是否周末\n",
    "df['IsWeekEnd'] = df['date'].apply(lambda x:\\\n",
    "     True if x.dayofweek in [5, 6] else False)  \n",
    "# 是否是工作时间\n",
    "# 支持自定义你的工作时间（朝九晚五｜007等）\n",
    "df.loc[((df['Hour'] >= 9) & (df['Hour'] < 18)), 'isWorking'] = True\n",
    "df[['date', 'WeekDay', 'Quarter', \\\n",
    "    \"Interval\", \"DayOfYear\", 'IsWeekEnd','isWorking']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2022-07-26 00:46:16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2022-07-26 00:46:18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2022-07-26 00:46:20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2022-07-30 16:01:39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2022-07-28 17:19:59</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2022-07-26 13:36:24</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  date\n",
       "0  2022-07-26 00:46:16\n",
       "1  2022-07-26 00:46:18\n",
       "2  2022-07-26 00:46:20\n",
       "3  2022-07-30 16:01:39\n",
       "4  2022-07-28 17:19:59\n",
       "5  2022-07-26 13:36:24"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 输入时间戳格式的数据👇\n",
    "df2 = pd.DataFrame(['1658767576',\"1658767578\",\\\n",
    "    \"1658767580\", '1659168099', \"1658999999\", \\\n",
    "        \"1658813784\"], columns=[\"date\"])\n",
    "# 转换为时间（默认为UTC时间，可以时区转换\n",
    "df2['date'] = pd.to_datetime(df2.date.values, unit = 's',\\\n",
    "    utc = True).tz_convert('Asia/Shanghai')\n",
    "df2['Hour'] = df2['date'].dt.hour\n",
    "# 不过上面这个方法非常难用，因为此时的时间依然是UTC格式存储\n",
    "# 如果用下面的方法，时区转换会便捷很多\n",
    "import time\n",
    "df2 = pd.DataFrame(['1658767576',\"1658767578\",\\\n",
    "    \"1658767580\", '1659168099', \"1658999999\", \\\n",
    "        \"1658813784\"], columns=[\"date\"])\n",
    "df2['date'] = df2['date'].map(lambda x: time.strftime(\"%Y-%m-%d %H:%M:%S\", \\\n",
    "    time.localtime(int(x))) )\n",
    "df2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.13 ('py38')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "da3096a4b2ddf6e3108983d9c4f8ed6b468b54d4f76a60924a980331cf3af984"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
